// header js

var $ = require('jquery');
var _common_util = require('util/common_util.js');
var _user_service = require('service/user-service.js');


var page = {
    init : function() {
        this.renderHtml();
        this.bindEvent();
    },
    renderHtml : function() {
        _user_service.checkLogin(
            function (data, msg) {
                $('.header-list .login').css('display', 'list-item');
                $('.header-list .login .username').text(data.username);
            },
            function (msg) {
                $('.header-list .no-login').css('display', 'list-item');
            }
        );
    },
    bindEvent : function () {
        this.bindHoverUserEvent();
        this.bindSiteNavEvent();
    },
    bindHoverUserEvent: function () {
        $(".header-item .user").mouseover(function () {
            $(".header-item .site-nav").removeClass("hidden");
            $(".header-item .site-nav").addClass("show");
        });
        $(".header-item .user").mouseleave(function () {
            $(".header-item .site-nav").removeClass("show");
            $(".header-item .site-nav").addClass("hidden");
        });
    },
    bindSiteNavEvent : function() {
        $(".nav-order").click(function () {
            window.open("order.html");
        });
        $(".nav-shipping").click(function () {
            window.open("shipping.html");
        });
        $(".nav-logout").click(function () {
            // window.open("order.html");
            _user_service.logout(
                function (data, msg) {
                    window.open("index.html", "_self");
                },
                function () {

                }
            );
            // window.open("index.html");
        });
    }
}

$(function() {
    page.init();
})